package com.zhanghu.backendZray.converter;

import com.zhanghu.backendZray.cache.ContextCache;
import com.zhanghu.backendZray.enums.NotifyTypeEnum;
import com.zhanghu.backendZray.pojo.entity.NotifySetting;
import com.zhanghu.backendZray.pojo.request.mail.NotifySettingRequest;
import com.zhanghu.backendZray.pojo.vo.mail.NotifySettingVo;
import org.springframework.stereotype.Component;

/**
 * @author ZhangDong
 * @date 2024/8/30 16:47
 */
@Component
public class NotifySettingConverter extends Converter<NotifySettingRequest, NotifySetting, NotifySettingVo> {

    protected NotifySettingConverter() {
        super(NotifySettingConverter::request2Entity, NotifySettingConverter::file2Vo);
    }

    private static NotifySetting request2Entity(NotifySettingRequest request) {
        NotifySetting setting = new NotifySetting();
        setting.setId(request.getId());
        Byte type = request.getType();
        setting.setType(type);
        setting.setUserId(ContextCache.currentUserId());
        if (NotifyTypeEnum.MAIL.getType().equals(type)) {
            setting.setMail(request.getMail());
        } else if (NotifyTypeEnum.BARK.getType().equals(type)) {
            setting.setBarkUrl(request.getBarkUrl());
        }
        return setting;
    }

    private static NotifySettingVo file2Vo(NotifySetting setting) {
        NotifySettingVo notifySettingVo = new NotifySettingVo();
        notifySettingVo.setId(setting.getId());
        notifySettingVo.setType(setting.getType());
        notifySettingVo.setMail(setting.getMail());
        notifySettingVo.setBarkUrl(setting.getBarkUrl());
        return notifySettingVo;
    }
}